package com.elitel.frame.base.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.elitel.frame.base.component.exportprojectdata.DataBaseConnInfo;
import com.elitel.frame.main.entity.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elitel.common.utils.XCommonUtils;
import com.elitel.frame.base.component.RedisCacheDataBase;
import com.elitel.frame.base.service.BaseEditconfigService;
import com.elitel.frame.main.dao.BaseEditconfigMapper;
import com.elitel.frame.main.dao.ext.ExtBaseEditconfigMapper;
import com.elitel.frame.main.entity.BaseEditconfig;
import com.elitel.frame.main.entity.ext.BaseEditconfigExt;

/**
 * @author guoyanfei
 * @Description 单表编辑服务(编辑信息配置)接口
 * @date 2018/01/04
 */
@Service
public class BaseEditconfigServiceImpl implements BaseEditconfigService {
    @Autowired
    private BaseEditconfigMapper baseEditconfigMapper;
    @Autowired
    private ExtBaseEditconfigMapper extBaseEditconfigMapper;
    @Autowired
    private RedisCacheDataBase redisCacheComponent;
    @Autowired
    private DataBaseConnInfo dataBaseConnInfo;

    @Override
    public Integer insertBaseEditconfig(String serviceId, String fieldIds) {
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer result = 0;
        int ordersort = 1;
        String databaseType = dataBaseConnInfo.dataBaseType;

        String[] fieldIdArray = fieldIds.split(",");
        for (String strfieldId : fieldIdArray) {
            BaseEditconfig baseEditconfig = new BaseEditconfig();
            Integer maxorder = 0;
            if (databaseType.equals("oracle")) {
                maxorder = extBaseEditconfigMapper.selectMaxshowOrderOracle(serviceId);

            } else if (databaseType.equals("mysql") || databaseType.equals("sqlite") || databaseType.equals(DataSourceType.POSTGRES)) {
                maxorder = extBaseEditconfigMapper.selectMaxshowOrderMysql(serviceId);
            }
            if (maxorder != null) {
                ordersort = maxorder + 1;
            }

            baseEditconfig.setGuid(UUID.randomUUID().toString());
            baseEditconfig.setServiceid(serviceId);
            baseEditconfig.setFieldid(strfieldId);
            baseEditconfig.setIscondition(0);
            baseEditconfig.setValidatecode("no");
            baseEditconfig.setCreatetime(new Date());
            baseEditconfig.setOrdersort(ordersort);
            result += baseEditconfigMapper.insertSelective(baseEditconfig);
        }

        //刷新缓存
        redisCacheComponent.refreshBaseEditconfigRedisCache(serviceId);
        return result;
    }

    //@Override
    public Integer batchAddBaseEditconfigList(String serviceId, List<BaseEditconfig> baseEditconfigList) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer result = 0;
        int ordersort = 1;
        String databaseType = dataBaseConnInfo.dataBaseType;

        for (BaseEditconfig baseEditconfig : baseEditconfigList) {

            Integer maxorder = 0;
            if (databaseType.equals("oracle")) {
                maxorder = extBaseEditconfigMapper.selectMaxshowOrderOracle(serviceId);

            } else if (databaseType.equals("mysql") || databaseType.equals("sqlite") || databaseType.equals(DataSourceType.POSTGRES)) {
                maxorder = extBaseEditconfigMapper.selectMaxshowOrderMysql(serviceId);
            }
            if (maxorder != null) {
                ordersort = maxorder + 1;
            }

            baseEditconfig.setGuid(UUID.randomUUID().toString());
            baseEditconfig.setServiceid(serviceId);
            if (baseEditconfig.getIscondition() == null)
                baseEditconfig.setIscondition(0);

            if (XCommonUtils.isEmpty(baseEditconfig.getValidatecode()))
                baseEditconfig.setValidatecode("no");

            if (XCommonUtils.isEmpty(baseEditconfig.getShowtype()))
                baseEditconfig.setShowtype("0");

            baseEditconfig.setCreatetime(new Date());
            baseEditconfig.setOrdersort(ordersort);
            result += baseEditconfigMapper.insertSelective(baseEditconfig);
        }

        //刷新缓存
        redisCacheComponent.refreshBaseEditconfigRedisCache(serviceId);
        return result;
    }


    @Override
    public List<BaseEditconfigExt> getAllBaseEditByServiceId(String serviceId) {
        return extBaseEditconfigMapper.getAllBaseEditByServiceId(serviceId);
    }

    @Override
    public Integer deleteBaseEditByguid(String guids) {
        Integer result = 0;
        String[] guidArray = guids.split(",");
        for (String str : guidArray) {
            BaseEditconfig baseEditconfig = baseEditconfigMapper.selectByPrimaryKey(str);
            result += baseEditconfigMapper.deleteByPrimaryKey(str);
            if (baseEditconfig != null) {
                //刷新缓存
                redisCacheComponent.refreshBaseEditconfigRedisCache(baseEditconfig.getServiceid());
            }
        }
        return result;
    }

    @Override
    public Integer updateBaseEdit(BaseEditconfig baseEditconfig) {
        Integer result = 0;
        result = baseEditconfigMapper.updateByPrimaryKeySelective(baseEditconfig);

        //刷新缓存
        redisCacheComponent.refreshBaseEditconfigRedisCache(baseEditconfig.getServiceid());
        return result;
    }


    @Override
    public Integer batchUpdateBaseEditList(List<BaseEditconfig> baseEditconfigList) {

        int count = 0;
        for (BaseEditconfig baseEditconfig : baseEditconfigList) {

            count += baseEditconfigMapper.updateByPrimaryKeySelective(baseEditconfig);

            //刷新缓存
            redisCacheComponent.refreshBaseEditconfigRedisCache(baseEditconfig.getServiceid());

        }
        return count;
    }


}
